package com.ruoyi.storage.service.cos;

import cn.hutool.core.util.ObjectUtil;
import com.aliyuncs.utils.IOUtils;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.storage.config.CosTenConfig;
import com.ruoyi.storage.enums.BizTypeEnum;
import com.ruoyi.storage.model.result.UploadFileResult;
import com.ruoyi.storage.service.ICosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qcloud.cos.COSClient;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Service
public class CosTenService implements ICosService {

    @Autowired
    CosTenConfig cosTenConfig;



    @Override
    public UploadFileResult uploadFile(MultipartFile file, String bizType) throws Exception {
        UploadFileResult uploadFileResult = new UploadFileResult();
        uploadFileResult.setSuccess(false);

        BizTypeEnum entityByBizType = BizTypeEnum.getEntityByBizType(bizType);
        if(ObjectUtil.isEmpty(entityByBizType)){
            return uploadFileResult;
        }
        String secretId = cosTenConfig.getSecretId();
        String secretKey = cosTenConfig.getSecretKey();
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域
        Region region = new Region(cosTenConfig.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);

        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端
        COSClient cosClient = new COSClient(cred, clientConfig);


        String bucketName = cosTenConfig.getBucketName();
        // 对象键(Key)是对象在存储桶中的唯一标识。
        String key = UUID.randomUUID().toString().replaceAll("-","")+
                file.getOriginalFilename();

//        String dateTime = new DateTime().toString("yyyy/MM/dd");
        key = StringUtils.format(BizTypeEnum.getEntityByBizType(bizType).getPath(), key);
//        key = dateTime +"/"+key;
        InputStream inputStream = null;

        try {

            inputStream = file.getInputStream();

            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType(file.getContentType());
            objectMetadata.setContentLength(file.getSize());
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream,objectMetadata);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            //获取返回路径
            String urlfile = "https://"+bucketName+".cos."+cosTenConfig.getRegion()+".myqcloud.com/"+key;
            uploadFileResult.setSuccess(true);
            uploadFileResult.setFileUrl(urlfile);
            return  uploadFileResult;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(inputStream);
        }

        return null;
    }
}
